package com.example.bookstore.controller.user;

import com.example.bookstore.entity.*;
import com.example.bookstore.service.BookService;
import com.example.bookstore.service.OrderService;
import com.example.bookstore.service.PeopleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Resource
    private BookService bookService;

    @Resource
    private PeopleService peopleService;

    @Resource
    private OrderService orderService;

    @RequestMapping("/addOne")
    public String addOne(Integer id,HttpSession session){
        Integer uid = (Integer) session.getAttribute("SESSION_USERID");

        //根据用户id和商品id查询购物车信息
        ShopCart shopCart = peopleService.findByTwo(uid,id);


        //如果查询结果为空，设置数量为1
        if (shopCart == null){
            Book book = bookService.findById(id);

            ShopCart shopCart1 = new ShopCart(0,id,uid,book.getName(),book.getImage(),1,book.getPrice(),book.getPrice());

            peopleService.add(shopCart1);
        }else {

            //如果查到结果，number++,并且在该类型商品总价上价格加一次
            peopleService.updateByUidAndIid(uid, id);
        }

        return "redirect:/findAll";
    }


    @RequestMapping("/queryShopCart")
     public String queryShopCart(HttpSession session,Model model){
        Integer id = (Integer) session.getAttribute("SESSION_USERID");
         List<ShopCart> list = peopleService.queryShopcart(id);
        Address address = peopleService.findAddressByUserId(id);
        double totalAmount = getTotalAmount(id);
            model.addAttribute("list",list);
            model.addAttribute("add",address);
            model.addAttribute("totalAmount",totalAmount);
            return "/pages/shopcart.jsp";
     }


    /**
     * 添加一个商品(在购物车页面)
     * @param iid
     * @param session
     * @return
     */
    @RequestMapping("/add1")
    public String add1(Integer iid,HttpSession session){
        Integer uid = (Integer) session.getAttribute("SESSION_USERID");

        //必查到结果，number++,并且在该类型商品总价上价格加一次
        peopleService.updateByUidAndIid(uid, iid);

        return "/user/queryShopCart";
    }

    /**
     * 减少一个商品(在购物车页面)
     * @param iid
     * @param session
     * @return
     */
    @RequestMapping("/less1")
    public String less1(Integer iid,HttpSession session){
        Integer uid = (Integer) session.getAttribute("SESSION_USERID");

        //必查到结果,该商品数减一,总价减一次。
        peopleService.updateByUidAndIid2(uid, iid);
        //根据用户id和商品id查询购物车信息,如果number==0，删除该条订单。
        ShopCart shopCart=peopleService.findByTwo(uid,iid);
        if (shopCart.getNumber()==0){
            peopleService.delete(shopCart.getId());
        }
        return "/user/queryShopCart";
    }


    //计算总价钱
    public double getTotalAmount(Integer id){
        List<ShopCart> list = peopleService.findSbAllShopCart(id);
        //遍历,用个数乘以金额，相加
        double totalAmount=0;
        for (ShopCart shopCart:list) {
            double price = shopCart.getPrice();
            Integer number = shopCart.getNumber();
            totalAmount+=price*number;
        }
        return totalAmount;
    }


    /**
     * 设置用户address
     * @param address
     * @param phone
     * @param session
     * @return
     */
    @RequestMapping("/setAddress")
    public String setAddress(String address,String phone,HttpSession session){
        Integer id = (Integer) session.getAttribute("SESSION_USERID");
        //判断用户address是否已经存在
        Address a = peopleService.findAddressByUserId(id);
        if (a!=null){
            //存在，更新地址
            peopleService.updateAddress(id,address,phone);
        }else {
            //不存在，设置地址
            peopleService.setAddress(id, address, phone);
        }
        return "redirect:/user/queryShopCart";
    }





}
